package com.ruoyi.edit.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.edit.domain.CourseContent;
import com.ruoyi.edit.domain.VO.CoursePackageVO;
import com.ruoyi.edit.utils.AssertUtil;
import com.ruoyi.edit.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.edit.mapper.CoursePackageMapper;
import com.ruoyi.edit.domain.CoursePackage;
import com.ruoyi.edit.service.ICoursePackageService;
import com.ruoyi.common.core.text.Convert;

/**
 * 课程包Service业务层处理
 * 
 * @author qiufeng
 * @date 2022-03-10
 */
@Service
public class CoursePackageServiceImpl implements ICoursePackageService 
{
    @Autowired
    private CoursePackageMapper coursePackageMapper;

    /**
     * 查询课程包
     * 
     * @param id 课程包主键
     * @return 课程包
     */
    @Override
    public CoursePackage selectCoursePackageById(String id)
    {
        return coursePackageMapper.selectCoursePackageById(id);
    }

    /**
     * 查询课程包列表
     * 
     * @param coursePackage 课程包
     * @return 课程包
     */
    @Override
    public List<CoursePackage> selectCoursePackageList(CoursePackage coursePackage)
    {
        return coursePackageMapper.selectCoursePackageList(coursePackage);
    }

    /**
     * 新增课程包
     * 
     * @param coursePackage 课程包
     * @return 结果
     */
    @Override
    public CoursePackage insertCoursePackage(CoursePackage coursePackage)
    {
        if (Objects.isNull(coursePackage.getPrice())){
            coursePackage.setPrice(Long.valueOf(0));
            coursePackage.setOriginPrice(Long.valueOf(0));
        }
        coursePackage.setId(UuidUtil.getUuid());
        coursePackage.setDeleted(0);
        coursePackage.setCreated(new Date());
        coursePackage.setCreatedBy(ShiroUtils.getLoginName());
        coursePackage.setCreatedName(ShiroUtils.getName());
        coursePackageMapper.insertCoursePackage(coursePackage);
        return coursePackage;
    }

    /**
     * 修改课程包
     * 
     * @param coursePackage 课程包
     * @return 结果
     */
    @Override
    public int updateCoursePackage(CoursePackage coursePackage)
    {
        return coursePackageMapper.updateCoursePackage(coursePackage);
    }

    /**
     * 批量删除课程包
     * 
     * @param ids 需要删除的课程包主键
     * @return 结果
     */
    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public int deleteCoursePackageByIds(String ids)
    {
        String[] ids1 = Convert.toStrArray(ids);
        for (int i = 0; i < ids1.length; i++) {
            CoursePackage coursePackage = coursePackageMapper.selectCoursePackageById(ids1[i]);
            if (AssertUtil.isNotEmpty(coursePackage)){
                coursePackage.setDeleted(1);
                coursePackageMapper.updateCoursePackage(coursePackage);
            }
        }
        return 1;
    }

    /**
     * 删除课程包信息
     * 
     * @param id 课程包主键
     * @return 结果
     */
    @Override
    public int deleteCoursePackageById(String id)
    {
        return coursePackageMapper.deleteCoursePackageById(id);
    }


    @Override
    public List<CoursePackageVO> selectCourseNameList(String name) {
        return coursePackageMapper.selectCourseNameList(name);
    }
}
